# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  
  protect_from_forgery # :secret => 'ae8762dc36ffd7d6db94fbbed6c79b1c'
  
   before_filter :initialize_user

  # make these available as ActionView helper methods.
  helper_method :logged_in? 
  helper_method :user_owns_record?

  protected
  
  def user_owns_record?
    logged_in? && @user.id == @current_user.id
  end

  # Check if the user is already logged in
  def logged_in? 
    @current_user.is_a?(User)
  end

  # setup user info on each page
  def initialize_user 
    @current_user = User.find_by_id(session[:user]) if session[:user]
  end
end
